import logging
import unittest
from common.dbutil import dbuilt
from config import BASE_DIR
from config import BODY
from api.login_api import LoginApi
from api.emp_api import EmpApi
from common import verify_assert_emp
from parameterized import parameterized
from common.read_json_data import build_data
from common.init_log import init_log_config


class TestEmp(unittest.TestCase):
    mobile = "17777888212"
    au_headers = None
    emp_id = None

    @classmethod
    def setUpClass(cls) -> None:
        print('类前置:删除指定员工')
        logging.info('类前置:删除指定员工')
        sql = f"delete from bs_user where mobile = '{cls.mobile}'"
        n = dbuilt.iud_data(sql)
        print(n)

        login_obj = LoginApi()
        body = BODY
        cls.au_headers = login_obj.get_au_headers(body)
        print(f'au_headers = {cls.au_headers}')
        logging.info(f'au_headers = {cls.au_headers}')

    @classmethod
    def tearDownClass(cls) -> None:
        print('类后置:删除指定员工')
        logging.info('类后置:删除指定员工')
        sql = f"delete from bs_user where mobile = '{cls.mobile}'"
        n = dbuilt.iud_data(sql)
        print(n)

    @parameterized.expand(build_data(BASE_DIR + '/data/emp.json'))
    def test_emp(self, case_name, add_body, modify_body, expect_result):
        add_body["mobile"] = self.mobile
        emp_obj = EmpApi()
        resposne = emp_obj.add_emp(add_body, self.au_headers)
        json_data = resposne.json()
        print(f'新增员工的结果为{json_data}')
        logging.info(f'新增员工的结果为{json_data}')
        verify_assert_emp.verify_emp_assert(self, expect_result, resposne)

        self.emp_id = json_data['data']['id']
        print(f'新增员工id为{self.emp_id}')
        logging.info(f'新增员工id为{self.emp_id}')

        response = emp_obj.query_emp(self.emp_id, self.au_headers)
        json_data = resposne.json()
        print(f'查询员工的结果为{json_data}')
        logging.info(f'查询员工的结果为{json_data}')
        verify_assert_emp.verify_emp_assert(self, expect_result, resposne)

        response = emp_obj.modify_emp(self.emp_id, modify_body, self.au_headers)
        json_data = resposne.json()
        print(f'删除员工的结果为{json_data}')
        logging.info(f'删除员工的结果为{json_data}')
        verify_assert_emp.verify_emp_assert(self, expect_result, resposne)

        response = emp_obj.delete_emp(self.emp_id, self.au_headers)
        json_data = resposne.json()
        print(f'删除员工的结果为{json_data}')
        logging.info(f'删除员工的结果为{json_data}')
        verify_assert_emp.verify_emp_assert(self, expect_result, resposne)
